package com.example.weatherpiano.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weatherpiano.DAO.RecWeather;
import com.example.weatherpiano.R;

import java.util.List;

public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.MyViewHolder> {

    private List<RecWeather> recWeatherList;
    private Context context;

    public MyRecycleViewAdapter(List<RecWeather> weatherBeanList, Context context) {
//         构造函数，接收数据和上下文
        this.recWeatherList = weatherBeanList;
        this.context = context;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//        加载布局
//        创建ViewHolder
        View view = View.inflate(context, R.layout.recent_weather, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyRecycleViewAdapter.MyViewHolder holder, int position) {
        holder.weather_icon.setImageResource(recWeatherList.get(position).getIconId());
        holder.date.setText(recWeatherList.get(position).getDate());
        holder.rec_temp.setText(recWeatherList.get(position).getCurrent_temp());
    }

    @Override
    public int getItemCount() {
        return recWeatherList.size();
    }

//    自定义方法，更新数据
    public void changeItems(List<RecWeather> recWeather) {
        recWeatherList = recWeather;
        notifyDataSetChanged();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
//        获取布局中的控件
        private ImageView weather_icon;
        private TextView date;
        private TextView rec_temp;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            weather_icon = itemView.findViewById(R.id.weather_icon);
            date = itemView.findViewById(R.id.date);
            rec_temp = itemView.findViewById(R.id.rec_temp);
        }
    }
}
